<link type="text/css" rel="stylesheet" href="/theme/<?=$_theme?>/common/css/!style.css"/>
<style type="text/css">
	.btnAdd,.btnDelete{display:none}
</style>
<script type="text/javascript">
<!--
function setMenu(){
	with(document.frmMenu){
		if(!menu_nm.value){
			alert("메뉴명을 입력해주세요");
			return;
		}
	}	

	$.ajax({
		type: "POST",
		url: "/chmanager/ajax_menu",
		data: $("[name=frmMenu]").serialize(),
		success: function(data){
			alert(data);
			
		},
		error:function(error){
			alert('에러');
		}
	});
}

function frmValid(){
	with(document.frmReg){
		submit();
	}
}

function frmDelete(){
	if(!confirm('삭제 하시겠습니까?'))return;
	with(document.frmReg){
		dbjob.value = 'd';
		submit();
	}	 
}

$(function () {
	$("[name=page_type]").change(function(){
		var contents_template='';
		switch($(this).val()){
			case("HTML"):
				contents_template = '/chmanager/contents/ajax_contents_html';
			break;
			case("BOARD"):
				contents_template = '/chmanager/contents/ajax_contents_board';
			break;
			case("GALLERY"):
				contents_template = '/chmanager/contents/ajax_contents_gallery';
			break;
			default:
				contents_template = '/chmanager/contents/ajax_contents_html';	
			break;
		}
		
		$.ajax({
			type: "POST",
			url: contents_template,
			data: "id=<?php echo $id?>",
			success: function(data){
				$("#contents-optional").html(data);
				
			},
			error:function(error){
				alert('에러');
			}
		});

	});
	
	if($("[name=dbjob]").val() == 'u') $(".btnDelete").show();
	if($("[name=id]").val()) $(".btnAdd").show();
	
	$("#demo")
		.delegate('a', 'click', function(e,data){
			
			location.replace("/chmanager/contents/list/id/"+$(this).parent("li").attr("id").replace('node_',''));
		}) 
		.bind("before.jstree", function (e, data) {
			$("#alog").append(data.func + "<br />");
		})
		.jstree({ 
			"themes" : {
				"theme" : "classic",
				"dots" : false,
				"icons" : false
			},		
			// List of active plugins
			"plugins" : [ 
				"themes","json_data","ui","crrm","cookies","dnd","search","types","hotkeys","contextmenu" 
			],
	
			// I usually configure the plugin that handles the data first
			// This example uses JSON as it is most common
			"json_data" : { 
				// This tree is ajax enabled - as this is most common, and maybe a bit more complex
				// All the options are almost the same as jQuery's AJAX (read the docs)
				"ajax" : {
					// the URL to fetch the data
					"url" : "/chmanager/contents/ajax_menu",
					// the `data` function is executed in the instance's scope
					// the parameter is the node being loaded 
					// (may be -1, 0, or undefined when loading the root nodes)
					"data" : function (n) { 
						// the result is fed to the AJAX request `data` option
						return { 
							"operation" : "get_children", 
							"id" : n.attr ? n.attr("id").replace("node_","") : 1 
						}; 
					}
				}
			},
			// Configuring the search plugin
			"search" : {
				// As this has been a common question - async search
				// Same as above - the `ajax` config option is actually jQuery's AJAX object
				"ajax" : {
					"url" : "/chmanager/contents/ajax_menu",
					// You get the search string as a parameter
					"data" : function (str) {
						return { 
							"operation" : "search", 
							"search_str" : str 
						}; 
					}
				}
			},
			// Using types - most of the time this is an overkill
			// read the docs carefully to decide whether you need types
			"types" : {
				// I set both options to -2, as I do not need depth and children count checking
				// Those two checks may slow jstree a lot, so use only when needed
				"max_depth" : -2,
				"max_children" : -2,
				// I want only `drive` nodes to be root nodes 
				// This will prevent moving or creating any other type as a root node
				"valid_children" : [ "drive" ],
				"types" : {
					// The default type
					"default" : {
						// I want this type to have no children (so only leaf nodes)
						// In my case - those are files
						"valid_children" : "none",
						// If we specify an icon for the default type it WILL OVERRIDE the theme icons
						"icon" : {
							"image" : "/theme/<?=$_theme?>/common/images/file.png"
						}
					},
					// The `folder` type
					"folder" : {
						// can have files and other folders inside of it, but NOT `drive` nodes
						"valid_children" : [ "default", "folder" ],
						"icon" : {
							"image" : "/theme/<?=$_theme?>/common/images/folder.png"
						}
					},
					// The `drive` nodes 
					"drive" : {
						// can have files and folders inside, but NOT other `drive` nodes
						"valid_children" : [ "default", "folder" ],
						"icon" : {
							"image" : "/theme/<?=$_theme?>/common/images/root.png"
						},
						// those prevent the functions with the same name to be used on `drive` nodes
						// internally the `before` event is used
						"start_drag" : false,
						"move_node" : false,
						"delete_node" : false,
						"remove" : false
					}
				}
			},
			// UI & core - the nodes to initially select and open will be overwritten by the cookie plugin
	
			// the UI plugin - it handles selecting/deselecting/hovering nodes
			"ui" : {
				// this makes the node with ID node_4 selected onload
				"initially_select" : [ "node_4" ]
			},
			// the core plugin - not many options here
			"core" : { 
				// just open those two nodes up
				// as this is an AJAX enabled tree, both will be downloaded from the server
				"initially_open" : [ "node_2" , "node_3" ] 
			}
		})
		.bind("create.jstree", function (e, data) {
			$.post(
				"/chmanager/contents/ajax_menu", 
				{ 
					"operation" : "create_node", 
					"id" : data.rslt.parent.attr("id").replace("node_",""), 
					"position" : data.rslt.position,
					"title" : data.rslt.name,
					"type" : data.rslt.obj.attr("rel")
				}, 
				function (r) {
					if(r.status) {
						$(data.rslt.obj).attr("id", "node_" + r.id);
					}
					else {
						$.jstree.rollback(data.rlbk);
					}
				}
			);
		})
		.bind("remove.jstree", function (e, data) {
			data.rslt.obj.each(function () {
				$.ajax({
					async : false,
					type: 'POST',
					url: "/chmanager/contents/ajax_menu",
					data : { 
						"operation" : "remove_node", 
						"id" : this.id.replace("node_","")
					}, 
					success : function (r) {
						if(!r.status) {
							data.inst.refresh();
						}
					}
				});
			});
		})
		.bind("rename.jstree", function (e, data) {
			$.post(
				"/chmanager/contents/ajax_menu", 
				{ 
					"operation" : "rename_node", 
					"id" : data.rslt.obj.attr("id").replace("node_",""),
					"title" : data.rslt.new_name
				}, 
				function (r) {
					if(!r.status) {
						$.jstree.rollback(data.rlbk);
					}
				}
			);
		})
		.bind("move_node.jstree", function (e, data) {
			data.rslt.o.each(function (i) {
				$.ajax({
					async : false,
					type: 'POST',
					url: "/chmanager/contents/ajax_menu",
					data : { 
						"operation" : "move_node", 
						"id" : $(this).attr("id").replace("node_",""), 
						"ref" : data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_",""), 
						"position" : data.rslt.cp + i,
						"title" : data.rslt.name,
						"copy" : data.rslt.cy ? 1 : 0
					},
					success : function (r) {
						if(!r.status) {
							$.jstree.rollback(data.rlbk);
						}
						else {
							$(data.rslt.oc).attr("id", "node_" + r.id);
							if(data.rslt.cy && $(data.rslt.oc).children("UL").length) {
								data.inst.refresh(data.inst._get_parent(data.rslt.oc));
							}
						}
						$("#analyze").click();
					}
				});
			});
		});
	
		//버튼 설정	
		$("#mmenu input").click(function () {
			switch(this.id) {
				case "add_default":
				case "add_folder":
					$("#demo").jstree("create", null, "last", { "attr" : { "rel" : this.id.toString().replace("add_", "") } });
					break;
				case "search":
					$("#demo").jstree("search", document.getElementById("text").value);
					break;
				case "text": break;
				default:
					$("#demo").jstree(this.id);
					break;
			}
		});
});
//-->
</script>
<form name="frmReg" method="post" action="/chmanager/contents">
<input type="hidden" name="id" value="<?=$id?>">
<input type="hidden" name="dbjob" value="<?php echo $dbjob?>">
	<div style="float:left;width:300px;border:1px solid red">	
		<div id="mmenu">
			<!--<input type="text" id="text" />  -->
			<input type="button" id="add_default" value="메뉴추가" />
		</div>
		<div id="demo" class="demo" style="min-height:500px;"></div>
	<!--<div id="mmenu" style="height:100px;">
			<input type="button" id="add_folder" value="add folder" style="display:block; float:left;"/>
			<input type="button" id="add_default" value="add file" style="display:block; float:left;"/>
			<input type="button" id="rename" value="rename" style="display:block; float:left;"/>
			<input type="button" id="remove" value="remove" style="display:block; float:left;"/>
			<input type="button" id="cut" value="cut" style="display:block; float:left;"/>
			<input type="button" id="copy" value="copy" style="display:block; float:left;"/>
			<input type="button" id="paste" value="paste" style="display:block; float:left;"/>
			<input type="button" id="clear_search" value="clear" style="display:block; float:right;"/>
			<input type="button" id="search" value="search" style="display:block; float:right;"/>
			<input type="text" id="text" value="" style="display:block; float:right;" />
		</div>  -->
	</div>
	
	<div style="float:left;width:800px;border:1px solid red">
		<table width="100%">
		<tr>
			<td>기능</td>
			<td>
				<select name="page_type">
				<?php
				foreach($this->config->item('ch_contents') as $key => $value){
					echo "<option value=\"$value\">$value</option>\n";
				}
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td>제목</td>
			<td>
				<input type="text" name="title" style="width:90%" value="<?php echo $title?>" />
			</td>
		</tr>
		<tr>
			<td>키워드</td>
			<td>
				<input type="text" name="keyword" style="width:90%" value="<?php echo $keyword?>" />
			</td>
		</tr>
		<tr>
			<td>설명</td>
			<td>
				<input type="text" name="description" style="width:90%" value="<?php echo $description?>" />
			</td>
		</tr>
		<tr>
			<td colspan="2" id="contents-optional">
				<textarea name="content" style="width:100%;height:500px"><?php echo $content?></textarea>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="button" class="btnAdd" value=" 저 장 " onclick="frmValid()" />
				<input type="button" class="btnDelete" value=" 삭 제 " onclick="frmDelete()" />
			</td>
		</tr>
		</table>
	</div>
</form>
<p style="both:clear"></p>

